package com.example.casper.Experiment2024.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;


import com.example.casper.Experiment2024.R;

import java.util.Calendar;

public class CustomClockView extends View {
    private Paint paint;
    private Bitmap clockFace;
    private Bitmap hourHand, minuteHand, secondHand;
    private int viewWidth;
    private int viewHeight;
    private int centerX;
    private int centerY;
    private int radius;

    public CustomClockView(Context context) {
        super(context);
        init();
    }

    public CustomClockView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomClockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        // 加载图片
        clockFace = BitmapFactory.decodeResource(getResources(), R.drawable.clock_face); // 表盘图片
        hourHand = BitmapFactory.decodeResource(getResources(), R.drawable.hour_hand); // 时针图片
        minuteHand = BitmapFactory.decodeResource(getResources(), R.drawable.minute_hand); // 分针图片
        secondHand = BitmapFactory.decodeResource(getResources(), R.drawable.second_hand); // 秒针图片
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewWidth = w;
        viewHeight = h;
        centerX = viewWidth / 2;
        centerY = viewHeight / 2;
        radius = Math.min(viewWidth, viewHeight) / 2 - 20; // 适当留出边距
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(clockFace, (centerX - clockFace.getWidth() / 2), (centerY - clockFace.getHeight() / 2), paint);
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE)+20;
        int second = calendar.get(Calendar.SECOND)+20;
        canvas.save();
        canvas.rotate((float) (hour * 30 + minute * 0.5), centerX, centerY);
        canvas.drawBitmap(hourHand, centerX - hourHand.getWidth() / 2, centerY - hourHand.getHeight() / 2, paint);
        canvas.restore();
        float minuteAngle = (float) ((minute + second / 60.0) * Math.PI / 30);
        canvas.save();
        canvas.rotate((float) (minute * 6), centerX, centerY);
        canvas.drawBitmap(minuteHand, centerX - minuteHand.getWidth() / 2, centerY - minuteHand.getHeight() / 2, paint);
        canvas.restore();
        float secondAngle = (float) (second * Math.PI / 30);
        canvas.save();
        canvas.rotate((float) (second * 6), centerX, centerY);
        canvas.drawBitmap(secondHand, centerX - secondHand.getWidth() / 2, centerY - secondHand.getHeight() / 2, paint);
        canvas.restore();
        invalidate();
    }
}